package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.common.util.OssUtil;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjFilterDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperFilterDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperSortDto;
import com.mysoft.gfyx.marketdevelop.service.CommonService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperCooperatorService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeveloperCooperatorServiceImp extends AppService implements DeveloperCooperatorService {

    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private LoadDataApiCommonService loadDataApiCommonService;

    @Autowired
    private CommonService commonService;

    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("uuid");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "developer_cooperator";
        fieldsSetting.getFields().add(new DataApiField("uuid", groupName, "RowID", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("developer_name", groupName, "开发商", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_name", groupName, "合作单位", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_organizing_code", groupName, "合作单位统一社会信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("organizing_code", groupName, "社会统一信用代码", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("category_name", groupName, "合作品类", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("project_level", groupName, "合作模式", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("project_name", groupName, "合作项目", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("begin_date", groupName, "合作时间", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadData(LoadDataParams options) {
        String creditCode = options.getOptions().getContext().get("id").toString();
        GetDeveloperFilterDataRequestDto dto = new GetDeveloperFilterDataRequestDto();

        dto = developerService.setCommonData(dto);
        dto.setDetail_table_name(DeveloperService.TABLE_COOPERATOR_WITH_INVESTMENT);
        dto.setPage(options.getPageIndex() + 1);
        dto.setPage_size(options.getPageSize());

        // dto.addFilterDtos("organizing_code", BaseTjFilterDto.OPERATE_EQUAL, creditCode, DeveloperService.TABLE_COOPERATOR, BaseTjFilterDto.ASSOCIATE_NONE);

        List<Filter> filters = options.getOptions().getFilters();
        //loadDataApiCommonService.setDefaultDeveloper(filters, creditCode);
        loadDataApiCommonService.setDefaultDeveloperWithoutInvestment(filters, creditCode);
        dto.addFilterByLoadDataApi(filters, DeveloperService.TABLE_COOPERATOR_WITH_INVESTMENT);

        // 排序
        ArrayList<GetDeveloperSortDto> sorts = new ArrayList<>();
        GetDeveloperSortDto sort = new GetDeveloperSortDto();
        sort.setSort_type(GetDeveloperSortDto.SORT_DESC);
        sort.setAttribute_name("begin_date");
        sort.setTable_name(DeveloperService.TABLE_COOPERATOR_WITH_INVESTMENT);
        sorts.add(sort);
        dto.setSorts(sorts);

        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(dto);

        return developerService.getTjResFromLoadData(res);
    }

    public Map<String, Object> getWinBidById(String id) {
        GetDeveloperFilterDataRequestDto dto = new GetDeveloperFilterDataRequestDto();

        dto = developerService.setCommonData(dto);
        dto.setDetail_table_name(DeveloperService.TABLE_WINBID_DW);
        dto.setPage(1);
        dto.setPage_size(1);
        dto.addFilterDtos("id", BaseTjFilterDto.OPERATE_EQUAL, id, DeveloperService.TABLE_WINBID_DW, BaseTjFilterDto.ASSOCIATE_NONE);

        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(dto);

        if (res.getData() == null || res.getData().getList() == null || res.getData().getList().size() == 0) {
            return new HashMap<>();
        }
        Map<String, Object> winBidDetail = res.getData().getList().get(0).getItem();
        //获取oss文件内容作为中标详情
        try {
            if (winBidDetail.get("publish_time") != null && winBidDetail.get("website_name") != null && winBidDetail.get("content_id") != null) {
                String filePath = OssUtil.getWinBidFilePath(winBidDetail.get("publish_time").toString(), winBidDetail.get("website_name").toString(), winBidDetail.get("content_id").toString());
                if (filePath.equals("")) {
                    winBidDetail.put("content_html", "");
                } else {
                    String contentHtml = OssUtil.getOssFile(filePath);
                    winBidDetail.put("content_html", contentHtml);
                }
            }
            //数据状态
            if (winBidDetail.get("id") != null) {
                winBidDetail.put("oppty_status", commonService.getOpptyStatus(CommonService.BigDataType.WINBID.toString(), winBidDetail.get("id").toString(), true));
            }
        } catch (IOException exception) {
            winBidDetail.put("content_html", "");
        }
        return winBidDetail;
    }

}
